package com.lekang.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.system.domain.CaregiverBedBinding;
import com.lekang.system.service.ICaregiverBedBindingService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 护理员与床位中间Controller
 *
 * @author zhihao
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/system/binding")
public class CaregiverBedBindingController extends BaseController
{
    @Autowired
    private ICaregiverBedBindingService caregiverBedBindingService;

    /**
     * 查询护理员与床位中间列表
     */
    @PreAuthorize("@ss.hasPermi('system:binding:list')")
    @GetMapping("/list")
    public TableDataInfo list(CaregiverBedBinding caregiverBedBinding)
    {
        startPage();
        List<CaregiverBedBinding> list = caregiverBedBindingService.selectCaregiverBedBindingList(caregiverBedBinding);
        return getDataTable(list);
    }

    /**
     * 导出护理员与床位中间列表
     */
    @PreAuthorize("@ss.hasPermi('system:binding:export')")
    @Log(title = "护理员与床位中间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaregiverBedBinding caregiverBedBinding)
    {
        List<CaregiverBedBinding> list = caregiverBedBindingService.selectCaregiverBedBindingList(caregiverBedBinding);
        ExcelUtil<CaregiverBedBinding> util = new ExcelUtil<CaregiverBedBinding>(CaregiverBedBinding.class);
        util.exportExcel(response, list, "护理员与床位中间数据");
    }

    /**
     * 获取护理员与床位中间详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:binding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(caregiverBedBindingService.selectCaregiverBedBindingById(id));
    }

    /**
     * 新增护理员与床位中间
     */
    @PreAuthorize("@ss.hasPermi('system:binding:add')")
    @Log(title = "护理员与床位中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaregiverBedBinding caregiverBedBinding)
    {
        return toAjax(caregiverBedBindingService.insertCaregiverBedBinding(caregiverBedBinding));
    }

    /**
     * 修改护理员与床位中间
     */
    @PreAuthorize("@ss.hasPermi('system:binding:edit')")
    @Log(title = "护理员与床位中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaregiverBedBinding caregiverBedBinding)
    {
        return toAjax(caregiverBedBindingService.updateCaregiverBedBinding(caregiverBedBinding));
    }

    /**
     * 删除护理员与床位中间
     */
    @PreAuthorize("@ss.hasPermi('system:binding:remove')")
    @Log(title = "护理员与床位中间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(caregiverBedBindingService.deleteCaregiverBedBindingByIds(ids));
    }

    /**
     * 根据床位ID查询护理员绑定信息
     */
    @PreAuthorize("@ss.hasPermi('system:binding:list')")
    @GetMapping("/bed/{bedId}")
    public AjaxResult getByBedId(@PathVariable Long bedId)
    {
        return success(caregiverBedBindingService.selectByBedId(bedId));
    }

    /**
     * 设置床位护理员
     */
    @PreAuthorize("@ss.hasPermi('system:binding:edit')")
    @Log(title = "设置床位护理员", businessType = BusinessType.UPDATE)
    @PostMapping("/setBedCaregivers")
    public AjaxResult setBedCaregivers(@RequestParam Long bedId, @RequestBody List<Long> caregiverIds)
    {
        return toAjax(caregiverBedBindingService.setBedCaregivers(bedId, caregiverIds));
    }

    /**
     * 批量设置床位护理员
     */
    @PreAuthorize("@ss.hasPermi('system:binding:edit')")
    @Log(title = "批量设置床位护理员", businessType = BusinessType.UPDATE)
    @PostMapping("/batchSetBedCaregivers")
    public AjaxResult batchSetBedCaregivers(@RequestParam String bedIds, @RequestBody List<Long> caregiverIds)
    {
        try {
            // 将逗号分隔的字符串转换为List
            List<Long> bedIdList = Arrays.stream(bedIds.split(","))
                    .map(Long::valueOf)
                    .collect(Collectors.toList());

            boolean result = caregiverBedBindingService.batchSetBedCaregivers(bedIdList, caregiverIds);
            return toAjax(result ? 1 : 0);
        } catch (Exception e) {
            return error("批量设置床位护理员失败：" + e.getMessage());
        }
    }

    /**
     * 移除床位的护理员
     */
    @PreAuthorize("@ss.hasPermi('system:binding:remove')")
    @Log(title = "移除床位护理员", businessType = BusinessType.DELETE)
    @DeleteMapping("/removeBedCaregiver")
    public AjaxResult removeBedCaregiver(@RequestParam Long bedId, @RequestParam Long caregiverId)
    {
        return toAjax(caregiverBedBindingService.removeBedCaregiver(bedId, caregiverId));
    }
}